package cn.studyjava.common.exception;

import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import cn.studyjava.common.Const;
import cn.studyjava.common.result.JsonResult;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author: zsljava
 * @version 1.0.0
 * <p>
 * History:
 * -----------------------------------------------
 * @Date: 2017年9月6日 上午11:11:37
 * @author: zsljava
 * @version 1.0.0
 * @OP: Create
 * -----------------------------------------------
 * </p>
 *
 * @since
 * @see
 */
@ControllerAdvice
public class SysExceptionHandler implements Serializable  {
	private static final long serialVersionUID = 1L;
	private Logger log = LoggerFactory.getLogger(SysExceptionHandler.class);
	
	@ResponseBody
	@ExceptionHandler(value = MethodArgumentNotValidException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public JsonResult<Object> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
		BindingResult bindingResult = ex.getBindingResult();
		ex.printStackTrace();
		log.error(bindingResult.getFieldError().getDefaultMessage());
		return new JsonResult<Object>(Const.REQ_FAIL, bindingResult.getFieldError().getDefaultMessage());
	}
	
//	@ResponseBody
//	@ExceptionHandler(value = RedisConnectionFailureException.class)
//	public JsonResult<Object> redisConnectionErrorHandler(HttpServletRequest req, Exception e) {
//		e.printStackTrace();
//		log.error(e.getMessage());
//		return new JsonResult<Object>(Const.VERIFY_STATUS_FAIL, VerifyEnum.SERVICE_REIDS_CONNECTION_FAIL.getCode(), VerifyEnum.SERVICE_REIDS_CONNECTION_FAIL.getMsg());
//	}
//	
//	@ResponseBody
//	@ExceptionHandler(value = NoHandlerFoundException.class)
//	public JsonResult<Object> noHandler(HttpServletRequest req, Exception e) {
//		e.printStackTrace();
//		log.error(e.getMessage());
//		return new JsonResult<Object>(Const.VERIFY_STATUS_FAIL, VerifyEnum.SERVER_REQ_PATH_NOT_FOUND.getCode(), e.getMessage());
//	}
//	
//	@ResponseBody
//	@ExceptionHandler(value = Exception.class)
//	public JsonResult<Object> defaultErrorHandler(HttpServletRequest req, Exception e) {
//		e.printStackTrace();
//		log.error(e.getMessage());
//		return new JsonResult<Object>(Const.VERIFY_STATUS_FAIL, VerifyEnum.VERIFY_AUTH_FAIL.getCode(), e.getMessage());
//	}
}
